home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / util / misc / ispell31.lha / ispell-3.1.18src / languages / fix8bit.c next >
C/C++ Source or Header  |  1994-01-25  |  6KB  |  212 lines

  1. #ifndef lint
  2. static char Rcs_Id[] =
  3.     "$Id: fix8bit.c,v 1.3 1994/01/25 07:12:26 geoff Exp $";
  4. #endif
  5.  
  6. /*
  7.  * Copyright 1993, Geoff Kuenning, Granada Hills, CA
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  *
  14.  * 1. Redistributions of source code must retain the above copyright
  15.  *    notice, this list of conditions and the following disclaimer.
  16.  * 2. Redistributions in binary form must reproduce the above copyright
  17.  *    notice, this list of conditions and the following disclaimer in the
  18.  *    documentation and/or other materials provided with the distribution.
  19.  * 3. All modifications to the source code must be clearly marked as
  20.  *    such.  Binary redistributions based on modified source code
  21.  *    must be clearly marked as modified versions in the documentation
  22.  *    and/or other materials provided with the distribution.
  23.  * 4. All advertising materials mentioning features or use of this software
  24.  *    must display the following acknowledgment:
  25.  *      This product includes software developed by Geoff Kuenning and
  26.  *      other unpaid contributors.
  27.  * 5. The name of Geoff Kuenning may not be used to endorse or promote
  28.  *    products derived from this software without specific prior
  29.  *    written permission.
  30.  *
  31.  * THIS SOFTWARE IS PROVIDED BY GEOFF KUENNING AND CONTRIBUTORS ``AS IS'' AND
  32.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34.  * ARE DISCLAIMED.  IN NO EVENT SHALL GEOFF KUENNING OR CONTRIBUTORS BE LIABLE
  35.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  36.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  37.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  39.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  40.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  41.  * SUCH DAMAGE.
  42.  */
  43.  
  44. /*
  45.  * This is a stupid little program that can be used to convert 8-bit
  46.  * characters to and from backslashed escape sequences.  It is usually
  47.  * more efficient to do this to affix files than to uuencode them for
  48.  * transport.  Ispell will read affix files in either format, so it is
  49.  * merely personal preference as to which form to use.
  50.  *
  51.  * Usage:
  52.  *
  53.  *    fix8bit {-7 | -8} < infile > outfile
  54.  *
  55.  * One of -7 and -8 must be specified.  If -7 is given, any character
  56.  * sequence that is not standard printable ASCII will be converted
  57.  * into a backslashed octal sequence.  If -8 is given, any backslashed
  58.  * octal or hex sequence will be converted into the equivalent 8-bit
  59.  * character.
  60.  *
  61.  * This program is not very smart.  In particular, it makes no attempt
  62.  * to understand comments, quoted strings, or similar constructs in
  63.  * which you might not want conversion to take place.  I suggest that
  64.  * you "diff" the input against the output, and if you don't like the
  65.  * result, correct it by hand.
  66.  */
  67.  
  68. /*
  69.  * $Log: fix8bit.c,v $
  70.  * Revision 1.3  1994/01/25  07:12:26  geoff
  71.  * Get rid of all old RCS log lines in preparation for the 3.1 release.
  72.  *
  73.  */
  74.  
  75. #include <stdio.h>
  76.  
  77. int        main ();    /* Convert to/from 8-bit sequences */
  78. static void    usage ();    /* Issue a usage message */
  79. static void    to7bit ();    /* Convert from 8-bit sequences */
  80. static void    to8bit ();    /* Convert to 8-bit sequences */
  81.  
  82. extern void    exit ();    /* Terminate program */
  83.  
  84. int main (argc, argv)        /* Convert to/from 8-bit sequences */
  85.     int            argc;    /* Argument count */
  86.     char *        argv[];    /* Argument vector */
  87.     {
  88.     
  89.     if (argc != 2)
  90.     usage ();
  91.     if (strcmp (argv[1], "-7") == 0)
  92.     to7bit ();
  93.     else if (strcmp (argv[1], "-8") == 0)
  94.     to8bit ();
  95.     else
  96.     usage ();
  97.     return 0;
  98.     }
  99.  
  100. static void usage ()        /* Issue a usage message */
  101.     {
  102.  
  103.     (void) fprintf (stderr, "Usage:  fix8bit {-7 | -8} < infile > outfile\n");
  104.     exit (1);
  105.     }
  106.  
  107. static void to7bit ()        /* Convert from 8-bit sequences */
  108.     {
  109.     int            ch;    /* Next character read from input */
  110.  
  111.     while ((ch = getchar ()) != EOF)
  112.     {
  113.     ch &= 0xFF;
  114.     if (ch >= 0200)
  115.         (void) printf ("\\%3.3o", (unsigned) ch);
  116.     else
  117.         (void) putchar (ch);
  118.     }
  119.     }
  120.  
  121. static void to8bit ()        /* Convert to 8-bit sequences */
  122.     {
  123.     int            backch;    /* Backslashed character being built */
  124.     int            ch;    /* Next character read from input */
  125.  
  126.     while ((ch = getchar ()) != EOF)
  127.     {
  128.     ch &= 0xFF;
  129.     if (ch != '\\')
  130.         (void) putchar (ch);
  131.     else
  132.         {
  133.         /*
  134.          * Collect a backslashed character.
  135.          */
  136.         ch = getchar ();
  137.         switch (ch)
  138.         {
  139.         case EOF:
  140.             (void) putchar ('\\');
  141.             return;
  142.         case 'x':
  143.         case 'X':
  144.             backch = ch;
  145.             ch = getchar ();
  146.             if (ch >= '0'  &&  ch <= '9')
  147.             backch = ch - '0';
  148.             else if (ch >= 'a'  &&  ch <= 'f')
  149.             backch = ch - 'a' + 0xA;
  150.             else if (ch >= 'A'  &&  ch <= 'F')
  151.             backch = ch - 'A' + 0xA;
  152.             else
  153.             {
  154.             (void) putchar ('\\');
  155.             (void) putchar (backch);
  156.             (void) putchar (ch);
  157.             break;
  158.             }
  159.             ch = getchar ();
  160.             if (ch >= '0'  &&  ch <= '9')
  161.             backch = (backch << 4) | (ch - '0');
  162.             else if (ch >= 'a'  &&  ch <= 'f')
  163.             backch = (ch << 4) - 'a' + 0xA;
  164.             else if (ch >= 'A'  &&  ch <= 'F')
  165.             backch = (ch << 4) - 'A' + 0xA;
  166.             else
  167.             {
  168.             (void) putchar (backch);
  169.             (void) putchar (ch);
  170.             break;
  171.             }
  172.             (void) putchar (backch);
  173.             break;
  174.         case '0':
  175.         case '1':
  176.         case '2':
  177.         case '3':
  178.         case '4':
  179.         case '5':
  180.         case '6':
  181.         case '7':
  182.             backch = ch - '0';
  183.             ch = getchar ();
  184.             if (ch >= '0'  &&  ch <= '7')
  185.             backch = (backch << 3) | (ch - '0');
  186.             else
  187.             {
  188.             (void) putchar (backch);
  189.             (void) putchar (ch);
  190.             break;
  191.             }
  192.             ch = getchar ();
  193.             if (ch >= '0'  &&  ch <= '7')
  194.             backch = (backch << 3) | (ch - '0');
  195.             else
  196.             {
  197.             (void) putchar (backch);
  198.             (void) putchar (ch);
  199.             break;
  200.             }
  201.             (void) putchar (backch);
  202.             break;
  203.             break;
  204.         default:
  205.             (void) putchar ('\\');
  206.             (void) putchar (ch);
  207.             break;
  208.         }
  209.         }
  210.     }
  211.     }
  212.